package com.xcc.shopanim;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.xcc.shopanim.utils.Logger;

/**
 * Created by Administrator on 2017/12/19.
 */
public class BaseActivity<T extends BaseContract.Presenter> extends SimpleBaseActivity<T> {
    /*private SwipeBackActivityHelper mHelper;//侧滑关闭

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mHelper = new SwipeBackActivityHelper(this);
        mHelper.onActivityCreate();

        if (BuildConfig.DEBUG) {
            SwipeBackLayout backLayout = getSwipeBackLayout();
            backLayout.addSwipeListener(new SwipeBackLayout.SwipeListener() {
                @Override
                public void onScrollStateChange(int state, float scrollPercent) {
                    Logger.out("onScrollStateChange");
                    if (state == 1) {//1开始 0停止
                        startColorCount();
                    } else if (state == 0) colorCountRunnable = null;
                }

                @Override
                public void onEdgeTouch(int edgeFlag) {
                }

                @Override
                public void onScrollOverThreshold() {
                }

                private Runnable colorCountRunnable;
                private SwipeBackLayout backLayout;
                private Field field = null;
                private int statusBarColor, statusBarColor2;
                private int navigationBarColor, navigationBarColor2;


                public void startColorCount() {
                    if (backLayout == null) {
                        backLayout = getSwipeBackLayout();
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            statusBarColor = getWindow().getStatusBarColor();
                            navigationBarColor = getWindow().getNavigationBarColor();
                            Activity activity = new Activity超高耦合位置搜索()._获取上一个未关闭的位置(getActivity());
                            if (activity != null) {
                                statusBarColor2 = activity.getWindow().getStatusBarColor();
                                navigationBarColor2 = activity.getWindow().getNavigationBarColor();
                            }
                        }
                        Field[] fields = SwipeBackLayout.class.getDeclaredFields();
                        for (int i = 0; i < fields.length; i++) {
                            if ("mScrollPercent".equals(fields[i].getName())) {
                                field = fields[i];
                                break;
                            }
                        }
                    }
                    colorCountRunnable = new Runnable() {
                        @Override
                        public void run() {
                            if (colorCountRunnable == null || this != colorCountRunnable) return;
                            if (isDestroy || isFinishing()) return;
                            if (field != null) {
                                field.setAccessible(true);
                                try {
                                    Object resultValue = field.get(backLayout);
                                    if (resultValue instanceof Float) {
                                        Float f = (Float) resultValue;
                                        //0xFF F6 F6 F6 当前
                                        //0xFF 51 9D 9E 目标
                                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                            int a1 = Color.alpha(statusBarColor);
                                            int a2 = Color.alpha(statusBarColor2);
                                            int r1 = Color.red(statusBarColor);
                                            int r2 = Color.red(statusBarColor2);
                                            int g1 = Color.green(statusBarColor);
                                            int g2 = Color.green(statusBarColor2);
                                            int b1 = Color.blue(statusBarColor);
                                            int b2 = Color.blue(statusBarColor2);
                                            float a = a1 - (a1 - a2) * f;
                                            float r = r1 - (r1 - r2) * f;
                                            float g = g1 - (g1 - g2) * f;
                                            float b = b1 - (b1 - b2) * f;
                                            int argb = Color.argb((int) a, (int) r, (int) g, (int) b);
                                            getWindow().setStatusBarColor(argb);
                                            a1 = Color.alpha(navigationBarColor);
                                            a2 = Color.alpha(navigationBarColor2);
                                            r1 = Color.red(navigationBarColor);
                                            r2 = Color.red(navigationBarColor2);
                                            g1 = Color.green(navigationBarColor);
                                            g2 = Color.green(navigationBarColor2);
                                            b1 = Color.blue(navigationBarColor);
                                            b2 = Color.blue(navigationBarColor2);
                                            a = a1 - (a1 - a2) * f;
                                            r = r1 - (r1 - r2) * f;
                                            g = g1 - (g1 - g2) * f;
                                            b = b1 - (b1 - b2) * f;
                                            argb = Color.argb((int) a, (int) r, (int) g, (int) b);
                                            getWindow().setNavigationBarColor(argb);
                                        }
                                    }
                                } catch (IllegalAccessException e) {
                                    e.printStackTrace();
                                }
                            }
                            backLayout.postDelayed(this, 50);
                        }
                    };
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        backLayout.postDelayed(colorCountRunnable, 50);
                    }
                }
            });
        }
    }

    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mHelper.onPostCreate();
        mHelper.getSwipeBackLayout().setScrimColor(0x0);
    }

    public View findViewById(int id) {
        View v = super.findViewById(id);
        if (v == null && mHelper != null)
            return mHelper.findViewById(id);
        return v;
    }

    public SwipeBackLayout getSwipeBackLayout() {
        return mHelper.getSwipeBackLayout();
    }

    public void setSwipeBackEnable(boolean enable) {
        getSwipeBackLayout().setEnableGesture(enable);
    }

    public void scrollToFinishActivity() {
        Utils.convertActivityToTranslucent(this);
        getSwipeBackLayout().scrollToFinishActivity();
    }

    public BaseActivity getActivity() {
        return this;
    }*/

    public static void removeView(View view) {
        if (view == null) return;
        ViewParent parent = view.getParent();
        if (parent instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) parent;
            viewGroup.removeView(view);
        }
    }

    //设置圆形单击按钮
    public static void setBackgroundOfClick(Context context, View view) {
        if (Build.VERSION.SDK_INT >= 18) {
            TypedValue mTypedValue_Borderless = new TypedValue();
            context.getTheme().resolveAttribute(R.attr.selectableItemBackgroundBorderless, mTypedValue_Borderless, true);
            view.setBackgroundResource(mTypedValue_Borderless.resourceId);
        }
    }

    /**
     * 更改shap类型的颜色
     * <p>
     * 注：这么做写起来爽，但是要做主题皮肤的话，那就GG了
     *
     * @param color 直接给颜色值，如：0xffF6F6F6
     */
    public static void setBackColor(View view, int color) {
        if (view == null) return;
        Drawable background = view.getBackground();
        if (background instanceof GradientDrawable) {
            GradientDrawable drawable = (GradientDrawable) background.mutate();
            drawable.setColor(color);
            view.setBackground(drawable);
        }
    }

    public class NetWorkStateReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            if (context == null) return;
            onNetworkType(getNetworkType(context));
        }
    }

    public static String getNetworkType(Context context) {
        String network;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            if (BuildConfig.DEBUG) {
                int type = networkInfo.getType();
                Logger.out("networkInfo.getType():" + type);
            }
            switch (networkInfo.getType()) {
                case ConnectivityManager.TYPE_WIFI:
                    network = "WIFI";
                    break;
                case ConnectivityManager.TYPE_MOBILE://手机网络
                case ConnectivityManager.TYPE_MOBILE_MMS://彩信
                case ConnectivityManager.TYPE_MOBILE_SUPL: //2G的一个连接方式
                case ConnectivityManager.TYPE_MOBILE_DUN: //2G的一个连接方式
                case ConnectivityManager.TYPE_MOBILE_HIPRI://可能是3G网络
                    network = "MOBILE";
                    break;
                case ConnectivityManager.TYPE_WIMAX://无线城域网
                    network = "WIMAX";
                    break;
                case ConnectivityManager.TYPE_BLUETOOTH://蓝牙
                    network = "BLUETOOTH";
                    break;
                case ConnectivityManager.TYPE_DUMMY://虚拟网络
                    network = "DUMMY";
                    break;
                case ConnectivityManager.TYPE_ETHERNET://以太网
                    network = "ETHERNET";
                    break;
                case ConnectivityManager.TYPE_VPN:
                    network = "VPN";
                    break;
                default:
                    network = "UNKNOWN";
                    break;
            }
        } else network = "DISCONNECT";//disconnect
        return network;
    }

    /**
     * 大于 1万 显示 1.00万
     */
    /*public static String getNumbTextStr(long numb) {
        if (numb > 10000) {
            String s = DoubleUtils.doubleDot2_(numb / 10000.0);
            String w = AppConstant.isZh() ? "万" : "W";
            return s + w;
        } else return String.valueOf(numb);
    }*/
}
